package cn.xiaowenjie.controllers;

import cn.xiaowenjie.beans.QwUser;
import cn.xiaowenjie.boss.form.LoginDto;
import cn.xiaowenjie.boss.form.QueryUserDTO;
import cn.xiaowenjie.common.beans.ResultBean;
import cn.xiaowenjie.helper.CryptoUtil;
import cn.xiaowenjie.response.PageResult;
import cn.xiaowenjie.services.UserManagerService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Api(description = "用户管理ADMIN api")
@RequestMapping("/user/admin")
@RestController
@CrossOrigin
@Slf4j
public class UserAdminController {
    @Autowired
    private UserManagerService userManagerService;

    @PostMapping("/all")
    public ResultBean<PageResult<QwUser>> getAll(@RequestBody QueryUserDTO pageObject) {
        return new ResultBean<PageResult<QwUser>>(userManagerService.getAll(pageObject));
    }

    /**
     * 新增配置
     * <p>
     * FIXME 同时支持json格式和表单格式
     */
    @PostMapping("/add")
    public ResultBean<Long> add(@RequestBody @Valid QwUser qwUser) {
        try {
            userManagerService.add(qwUser);
            return new ResultBean<Long>(1L);
        } catch (Exception e) {
            return new ResultBean<Long>(0L);
        }
    }

    @PostMapping("/delete")
    public ResultBean<Boolean> delete(@RequestParam Long id) {
        try {
            userManagerService.delete(id);
            return new ResultBean<Boolean>(true);
        } catch (Exception e) {
            return new ResultBean<Boolean>(false);
        }
    }

    @PostMapping("/update")
    public ResultBean<Long> update(@RequestBody @Valid QwUser advert) {
        try {
            userManagerService.update(advert);
            return new ResultBean<Long>(1L);
        } catch (Exception e) {
            return new ResultBean<>(0L);
        }
    }

    @PostMapping("/login")
    public ResultBean login(@RequestBody LoginDto loginDto) {
        ResultBean resultBean = new ResultBean();
        log.info("userName is {},password is {}", loginDto.getName(), loginDto.getPassword());
        if(loginDto.getName()==null || loginDto.getPassword()==null){
            resultBean.setCode(-1);
            resultBean.setMsg("参数不正确，请确认请求参数");
            return resultBean;
        }
        QwUser qwUser = userManagerService.queryByUserName(loginDto.getName());
        if(qwUser==null || qwUser.getId()<=0){
            resultBean.setCode(-1);
            resultBean.setMsg("根据该账号未查询到用户信息，请确认用户名称是否正确");
            return resultBean;
        }
        if(!loginDto.getPassword().equals(CryptoUtil.decode(qwUser.getPassword()))){
            resultBean.setCode(-1);
            resultBean.setMsg("密码不正确，请确认输入的密码是否正确");
            return resultBean;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("userName",qwUser.getName());
        map.put("menuView",qwUser.getMenuView());
        resultBean.setCode(1);
        resultBean.setData(map);
        return resultBean;
    }
}
